<?php
/*
 * ITHI MVC framework
*/
//require_once(BASE_PATH.'/librarys/adodb/adodb.inc.php');
require_once(BASE_PATH.'/librarys/tbs_class_php5.php');
include_once(BASE_PATH.'/librarys/mysqlBase.class.php');
include_once(BASE_PATH.'/librarys/Request.class.php');
class ITHICore
{
    protected  $db;
    public  $template;
    protected  $request;
    protected  $cache;
    private  $controller;
    private  $action;
    public $__useDB = true;

    public  function __construct()
    {
        $this->db = Registry::get('db');
        $this->cache = Registry::get('cache');
        $this->template = new clsTinyButStrong();
        $this->request = new Request();
        $this->getPath();
        $this->loadController();
        $this->setTemplate();
    }
    
    private function getPath() {
        if (Registry::isRegistered('controller') and Registry::get('controller')) {
            $this->controller = Registry::get('controller');
            $this->action = Registry::get('action');
            Registry::set('controller', false);
            Registry::set('action', false);
            return;
        }
        $path = str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['REQUEST_URI']);
        if ($path == $_SERVER['REQUEST_URI']) { //user request the root without "index.php"
            $path = '';
        }
        $path = explode('/', $path);
        $this->controller = str_replace(strstr(@$path[1], "?"), "", @$path[1]);
        $this->action = str_replace(strstr(@$path[2], "?"), "", @$path[2]);
        if (empty($this->controller)) {
            $this->controller = 'index';
        }
        if (empty($this->action)) {
            $this->action = 'index';
        }
    }
	
    private function setTemplate() {
        if (!file_exists(BASE_PATH."/app/{$this->controller}/views/{$this->action}.html")) {
            $error = 'Can not find this Views template: '. BASE_PATH."/app/{$this->controller}/views/{$this->action}.html";
            throw new Exception($error);
        }
       $this->template->LoadTemplate(BASE_PATH."/app/{$this->controller}/views/{$this->action}.html");
    }
    
    protected function loadController() {
        if (!file_exists(BASE_PATH."/app/{$this->controller}/{$this->controller}Controller.php")) {
           $error = 'Can not find this Controller: ' .BASE_PATH."/app/{$this->controller}/{$this->controller}Controller.php";
           throw new Exception($error);
        }
       require_once(BASE_PATH."/app/{$this->controller}/{$this->controller}Controller.php");
    }

    public function setAction($controller = null, $action = null) {
        if ($controller) {
            $this->controller = $controller;
        }
        if ($action) {
            $this->action = $action;
        }
        Registry::set('action', $this->action);
        Registry::set('controller', $this->controller);
        $this->loadController();
        return $this->init();
    }

    

    //Init the APP
    public  function init()
    {   
        if (DEBUG_MODE) {
            echo '<hr />the controller is '.$this->controller;
            echo '<hr />the action is '.$this->action;
        }
        
        $Controller = new $this->controller;
        $output = $Controller->{$this->action.'Action'}();
        return $output;
    }

    //URL redirect
    function goto($url)
        {
        header("localtion:{$url}");
    }
    
    protected function getOutput() {
        $var_output['error'] = get_msg('error');
        $var_output['msg'] = get_msg('msg');
        $this->template->MergeField('var_output', $var_output);
        return $this->template->Show();
    }
    //unset
    function __destruct()
    {
        unset($this);
    }
}
?>
